package com.levy.contronller.lamdaTest;

import org.junit.Test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * Created by levy on 2019/5/14.
 */
public class LamdaTest {

    public static List<Person> list = getPersonList();

    @Test
    public void test1() {

        new Thread(() ->
                System.out.println("2222222222")
        ).start();

//        new Thread(() ->
//                System.out.println("33333333")
//        ).start();

    }

    @Test
    public void test2() {
        list.forEach(p -> System.out.println(p.toString()));
    }


    @Test
    public void test3() {
        Consumer<Person> changeAge = p -> p.setAge(p.getAge() + 3);
        list.forEach(changeAge);
        list.forEach(System.out::println);
    }

    @Test
    public void test4() {
        list.stream().filter(e -> e.getAge() > 20)
                .forEach(e -> System.out.println(e.toString()));
    }


    @Test
    public void test5() {
        Predicate<Person> ageFilter = e -> e.getAge() > 20;
        Predicate<Person> sexFilter = e -> e.getSex().equals("male");

        //多条件过滤
        list.stream().filter(ageFilter)
                .filter(sexFilter)
                .forEach(e -> System.out.println(e.toString()));

        //predicate :and Or
        list.stream().filter(ageFilter.and(sexFilter))
                .forEach(e -> System.out.println(e.toString()));
    }


    @Test
    public void test6() {
        list.stream().limit(3)
                .forEach(e -> System.out.println(e.toString()));

        list.stream().limit(2)
                .filter(e -> e.getAge() > 20)
                .forEach(e -> System.out.println(e.toString()));
    }

    @Test
    public void test7() {
        //年龄排序
        list.stream().sorted((p1, p2) -> (p1.getAge() - p2.getAge()))
                .forEach(e -> System.out.println(e.toString()));
        //姓名排序

        list.stream().sorted(Comparator.comparing(person -> person.getName()))
                .forEach(e -> System.out.println(e.toString()));

        list.stream().sorted(Comparator.comparing(Person::getName))
                .forEach(e -> System.out.println(e.toString()));
    }

    @Test
    public void test8() {
        Optional<Person> max = list.stream().max(Comparator.comparing(Person::getAge));
        System.out.println(max.toString());
    }


    @Test
    public void test9() {
//        list.stream().map(e -> e.setAge(e.setAge(123)))
//                .forEach(e -> System.out.println(e.toString()));


    }


    @Test
    public void test10() {
        List<Person> personList = getPersonList();
        Collector<Person, ?, String> mapping = Collectors.mapping(Person::getName, Collectors.joining(", ", "name: [", "]"));
        Map<String, String> collect = personList.stream()
                .collect(Collectors.groupingBy(Person::getSex, mapping));
        System.out.println(collect);

    }

    @Test
    public void test11() {
        List<Person> personList = getPersonList();
        Collector<Person, ?, List<String>> mapping = Collectors.mapping(Person::getName, Collectors.toList());
        Map<String, List<String>> collect = personList.stream()
                .collect(Collectors.groupingBy(Person::getSex, mapping));
        System.out.println(collect);

    }

    @Test
    public void test12() {
        List<Person> personList = getPersonList();
        Collector<Person, ?, Set<String>> mapping = Collectors.mapping(Person::getName, Collectors.toSet());
        Map<String, Set<String>> collect = personList.stream()
                .collect(Collectors.groupingBy(Person::getSex, mapping));
        System.out.println(collect);

    }





    private static List<Person> getPersonList() {
        Person p1 = new Person("liu", 22, "male");
        Person p2 = new Person("zhao", 21, "male");
        Person p3 = new Person("li", 18, "female");
        Person p4 = new Person("wang", 21, "female");
        Person p5 = new Person("li", 18, "female");
        List<Person> list = new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        return list;
    }


}

class Person {
    private String name;
    private int age;
    private String sex;

    public Person(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}






